package mms.farina.thecell.support;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

/**Classe che implementa le funzioni crittografiche utilizzate*/
public class CryptoFunction {


	private static String convertToHex(byte[] data) { 
		StringBuilder buf = new StringBuilder();
		for (int i = 0; i < data.length; i++) { 
			int halfbyte = (data[i] >>> 4) & 0x0F;
			int two_halfs = 0;
			do { 
				if ((0 <= halfbyte) && (halfbyte <= 9)) 
					buf.append((char) ('0' + halfbyte));
				else 
					buf.append((char) ('a' + (halfbyte - 10)));
				halfbyte = data[i] & 0x0F;
			} while(two_halfs++ < 1);
		} 
		return buf.toString();
	}
	
	public static byte[] hexStringToByteArray(String s) {
	    int len = s.length();
	    byte[] data = new byte[len / 2];
	    for (int i = 0; i < len; i += 2) {
	        data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
	                             + Character.digit(s.charAt(i+1), 16));
	    }
	    return data;
	}


	public static String SHA256(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException  { 
		MessageDigest md = MessageDigest.getInstance("SHA-256");        
		md.update(text.getBytes("iso-8859-1"), 0, text.length());
		byte[] sha1hash = md.digest();
		return convertToHex(sha1hash);
	}

	public static String MD5(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException  { 
		MessageDigest md = MessageDigest.getInstance("MD5");        
		md.update(text.getBytes("iso-8859-1"), 0, text.length());
		byte[] sha1hash = md.digest();
		return convertToHex(sha1hash);
	}
	
	public static String MD5(byte[] b) throws NoSuchAlgorithmException, UnsupportedEncodingException  { 
		MessageDigest md = MessageDigest.getInstance("MD5");        
		md.update(b, 0, b.length);
		byte[] sha1hash = md.digest();
		return convertToHex(sha1hash);
	} 

	public static String AES_Encrypt(String text, String keyS) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
		
		byte[] key=keyS.getBytes();
		SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
		Cipher cipher = Cipher.getInstance("AES");

		cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

		byte[] encrypted =cipher.doFinal(text.getBytes());
		
		return convertToHex(encrypted);
		
	}

	public static String AES_Decrypt(String text, String keyS) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
		
		byte[] key=keyS.getBytes();
		byte[] encrypted = hexStringToByteArray(text);
		SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
		Cipher cipher = Cipher.getInstance("AES");
		
		cipher.init(Cipher.DECRYPT_MODE, skeySpec);
	    byte[] original = cipher.doFinal(encrypted);
	    
	    String originalString = new String(original);
	       
	    return originalString;

	}
	
	public static byte[] AES_Encrypt_B(String text, String keyS) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
		
		byte[] key=keyS.getBytes();
		SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
		Cipher cipher = Cipher.getInstance("AES");

		cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

		byte[] encrypted =cipher.doFinal(text.getBytes());
		
		return encrypted;
		
	}

	public static byte[] AES_Decrypt_B(String text, String keyS) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
		
		byte[] key=keyS.getBytes();
		byte[] encrypted = hexStringToByteArray(text);
		SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
		Cipher cipher = Cipher.getInstance("AES");
		
		cipher.init(Cipher.DECRYPT_MODE, skeySpec);
	    byte[] original = cipher.doFinal(encrypted);
	    
	    return original;

	}
	
	

}
